package org.keyroy.map_editor.ui.components;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

class SmallMapComponent extends JComponent {
	public static final Color RECT_COLOR = new Color(0xffff6600, false);
	private static final long serialVersionUID = 1L;
	private BufferedImage bufferedImage;
	private Rectangle rectangle;
	private double scale;

	public SmallMapComponent(final MapComponent mapComponent, final SmallMapListener mapListener) {
		rectangle = new Rectangle();

		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
			}
		});

		MouseAdapter mouseAdapter = new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				mapListener.onMousePressed((int) ((double) e.getX() / scale), (int) ((double) e.getY() / scale));
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				mapListener.onMousePressed((int) ((double) e.getX() / scale), (int) ((double) e.getY() / scale));
			}
		};

		addMouseListener(mouseAdapter);
		addMouseMotionListener(mouseAdapter);
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (bufferedImage != null) {
			int ox = (getWidth() - bufferedImage.getWidth()) / 2;
			int oy = (getHeight() - bufferedImage.getHeight()) / 2;
			g.drawImage(bufferedImage, ox, oy, null);
			g.setColor(RECT_COLOR);
			g.drawRect(rectangle.x + ox, rectangle.y + oy, rectangle.width, rectangle.height);
		}
	}

	public void updateMap() {
		if (bufferedImage != null) {
		}
	}

	public interface SmallMapListener {
		public void onMousePressed(int x, int y);
	}

}
